package cn.kgc.controller;

import cn.kgc.base.BaseInfoProperties;
import cn.kgc.entity.ChatMsg;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.mapper.ChatMsgMapper;
import cn.kgc.service.ChatMsgService;
import cn.kgc.utils.PagedGridResult;
import cn.kgc.utils.RedisUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author YC
 */
@RestController
@RequestMapping("/chat")
@Api(tags = "聊天管理模块")
public class ChatController {

    @Resource
    private ChatMsgMapper chatMsgMapper;
    @Resource
    private ChatMsgService chatMsgService;
    @Autowired
    private RedisUtil redisUtil;

    @PostMapping("/page")
    @ApiOperation(value = "分页查询历史聊天记录")
    public PagedGridResult page(
            @RequestParam("receiverId") String receiverId,
            @RequestParam("senderId") String senderId,
            @RequestParam("pageIndex") Integer pageIndex,
            @RequestParam("pageSize") Integer pageSize
    ) {
        LambdaQueryWrapper<ChatMsg> wrapper = new LambdaQueryWrapper<ChatMsg>()
                .eq(ChatMsg::getReceiverId, receiverId)
                .eq(ChatMsg::getSenderId, senderId)
                .orderByDesc(ChatMsg::getChatTime);
        Page<ChatMsg> page = new Page<>(pageIndex, pageSize);
        chatMsgMapper.selectPage(page, wrapper);
        List<ChatMsg> records = page.getRecords();
        // 按照聊天时间排序
        records.sort((o1, o2) -> {
            long o1Time = o1.getChatTime().getTime();
            long o2Time = o2.getChatTime().getTime();
            return Long.compare(o1Time, o2Time);
        });
        for (ChatMsg chatMsg : records) {
            // 判断客户端是否显示聊天时间，发送时间与读取时间间隔超过3分钟，则显示时间
            if (chatMsg.getChatTime().getTime() - System.currentTimeMillis() > 3 * 60 * 1000) {
                chatMsg.setShowMsgDateTimeFlag(1);
            }
        }
        page.setRecords(records);
        return PagedGridResult.setterPagedGrid(page);
    }

    @PostMapping("/noReadCount")
    @ApiOperation("获取用户未读消息数量")
    public GraceJSONResult getUserNoReadCount(String userId) {
        String key = BaseInfoProperties.REDIS_UNREAD_MSG_COUNT + userId;
        return GraceJSONResult.ok(redisUtil.hGetAll(key));
    }

    @PostMapping("/cleanReadCount")
    @ApiOperation("获取用户未读消息数量")
    public GraceJSONResult cleanUserReadCount(String receiverId, String senderId) {
        String key = BaseInfoProperties.REDIS_UNREAD_MSG_COUNT + senderId;
        return GraceJSONResult.ok(redisUtil.delete(key, senderId));
    }

    @PostMapping("/changeAudioRead")
    @ApiOperation("点击语言消息后状态已读")
    public GraceJSONResult changeAudioRead(String msgId) {
        return GraceJSONResult.ok(chatMsgService.hasRead(msgId));
    }

}
